//
//  XTStatusPictrueView.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/18.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit

/*
    1.创建9个imageView
    2.根据url的布局位置显示图片,不显示图片的imageView隐藏掉
*/


//imageView之间的间距
let imageViewMargin : CGFloat = 4


class XTStatusPictrueView: UIView {
    
    
    //MARK: - 属性
    
    //图片的url
    var pictureURLs : [NSURL]?{
        
        didSet {
        
            let height = layoutImageView()
            
            //重新更新约束
            self.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(height)
                
            }
 
        }
    }

    
    func layoutImageView() -> CGFloat {
        
        let count = pictureURLs?.count ?? 0
        //            XTPrint(items: "count = \(count)")
        debugLabel.text = "\(count)"
        
        //最大列数,行数
        let maxColumnAndRow : CGFloat = 3
        
        //设置宽高
        let WH = (UIScreen.mainScreen().bounds.width - 2 * cellMargin - (maxColumnAndRow - 1) * imageViewMargin) / maxColumnAndRow
        
        //当前列数
        var numberOfColumn = 3
        if count == 4 || count == 2 {
            
             numberOfColumn = 2
        }else if count == 1 {
            
            numberOfColumn = 1
        }
        
        
        var index = 0
        //遍历每一个imageView 计算frame
        for imageView in imageViews {
            
            if index >= count {
                
                imageView.hidden = true
            }else {
                
                imageView.hidden = false
                
                //计算在第几行
                let row = index / numberOfColumn
                
                //计算在第几列
                let column = index % numberOfColumn
                
                //设置frame
                imageView.frame = CGRect(x: CGFloat(column) * (WH + imageViewMargin), y: CGFloat(row) * (WH + imageViewMargin), width: WH, height: WH)
                
                //给imageView赋值
                imageView.sd_setImageWithURL(pictureURLs![index])
            }
            index++
        }
        
        //计算配图的大小
        if count == 0 {
            return 0
        }
        
        // 总行数
        let numOfRow = (count + numberOfColumn - 1) / numberOfColumn
        
        //配图高度
        let height = CGFloat(numOfRow) * WH + (CGFloat(numOfRow) - 1) * imageViewMargin
        
        return height
        
    }
    
    
    ///储存9个imageView
    private var imageViews = [UIImageView]()
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
     
        
        //设置UI
        prepareUI()
        
        debugLabel.hidden = true
        
    }

    private func prepareUI() {
    
//        self.backgroundColor = UIColor.magentaColor()
        
        //创建9个imageview
        for _ in 0..<9 {
        
            let imageView = UIImageView()
            imageView.contentMode = UIViewContentMode.ScaleAspectFill
            imageView.clipsToBounds = true
            //随机颜色
            imageView.backgroundColor = UIColor.random()
            //添加
            self.addSubview(imageView)
            
            //将imageView添加到数组中储存
            imageViews.append(imageView)
            
        }
        
        
        //debugLabel添加到视图中
        addSubview(debugLabel)
        
    }
    
    
    //MARK: - 懒加载
    private lazy var debugLabel : UILabel = {
        let label = UILabel()
        
        //设置
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        
        label.frame = CGRect(x: 10, y: 10, width: 100, height: 50)
        
        
        return label
    }()
    
    
    

}
